package com.litlum.fibermana.controller;

import com.litlum.fibermana.dto.PalletDTO;
import com.litlum.fibermana.entity.Pallet;
import com.litlum.fibermana.service.IPalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/pallet")
public class PalletController {

    @Autowired
    private IPalletService palletService;

    @PostMapping("/create")
    public String createPallet(@RequestBody PalletDTO palletDTO) {
        palletService.save(palletDTO.toPallet());
        return "Successfully created pallet";
    }

    @GetMapping("/get_all")
    public List<PalletDTO> getAllPallets() {
        List<Pallet> pallets = palletService.findAll();
        List<PalletDTO> palletDTOs = new ArrayList<>();
        for (Pallet pallet : pallets) {
            PalletDTO palletDTO = PalletDTO.fromPallet(pallet);
            palletDTOs.add(palletDTO);
        }
        return palletDTOs;
    }
}
